from django.shortcuts import render
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
from rest_framework.response import Response
import rest_framework.status as status
from projectDesign.models import styleSheet
from projectManage.models import Project
from client.models import UserAccount
from django.utils import timezone
# Create your views here.


class exportStyleSheet(APIView):
    permission_classes = [IsAuthenticated]

    def post(self, request):
        try:
            user_insance = UserAccount.objects.get(id=request.user.id)
            styleSheet.objects.create(
                name=request.data['name'], intro=request.data['intro'], designer=user_insance, styleCSS=request.data['styleCss'], label=request.data['label'])
            return Response({'message': '导出样式表成功！'}, status=status.HTTP_200_OK)
        except Exception as e:
            return Response({'message': str(e)}, status=status.HTTP_400_BAD_REQUEST)

class deleteStyleSheet(APIView):
    permission_classes = [IsAuthenticated]

    def post(self, request):
        try:
            styleSheet.objects.filter(designer_id=request.user.id, name=request.data['name']).delete()
            return Response({'message': '删除样式表成功！'}, status=status.HTTP_200_OK)
        except Exception as e:
            return Response({'message': str(e)}, status=status.HTTP_400_BAD_REQUEST)

class getAllStyleSheets(APIView):
    permission_classes = [IsAuthenticated]

    def get(self, request):
        try:
            styleSheets = styleSheet.objects.filter(designer_id=request.user.id).values()
            names = [(lambda x: x['name'])(x) for x in styleSheets]
            return Response({'style': styleSheets, 'name': names}, status=status.HTTP_200_OK)
        except Exception as e:
            return Response({'message': str(e)}, status=status.HTTP_400_BAD_REQUEST)

class exportPageCode(APIView):
    permission_classes = [IsAuthenticated]

    def post(self, request):
        try:
            # Project.objects.filter(user_id=1).delete()
            project = Project.objects.get(user_id=request.user.id, project_id=int(request.data['project_id']))
            project.htmlCode = request.data['html']
            project.jsCode = request.data['js']
            project.domTreeMap = request.data['domTreeMap']
            project.count = request.data['count']
            project.modify_time = timezone.now()
            project.save()
            return Response({'message': '导出代码成功！'}, status=status.HTTP_200_OK)
        except Exception as e:
            return Response({'message': str(e)}, status=status.HTTP_400_BAD_REQUEST)

class getPageCode(APIView):
    permission_classes = [IsAuthenticated]

    def get(self, request):
        try:
            project_id = int(request.GET.get('project_id'))
            project = Project.objects.filter(user_id=request.user.id, project_id=project_id).values()
            return Response({'project': project}, status=status.HTTP_200_OK)
        except Exception as e:
            return Response({'message': str(e)}, status=status.HTTP_400_BAD_REQUEST)
